package no.ntnu.item.ttm4115.termproject.groupchatagent;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.thoughtworks.xstream.XStream;

import no.ntnu.item.ttm4115.termproject.InitiateNewGroupChat;
import no.tellu.common.javaframe.ActorAddress;
import no.tellu.common.javaframe.messages.RoleRequestMsg;
import no.tellu.common.actorframe.messages.RoleConfirmMsg;
import no.ntnu.item.ttm4115.termproject.EnableGCAforGroup;
import no.ntnu.item.ttm4115.termproject.GCAGstartedToGa;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.groupchatagent.gui.GroupChatGUI;
import no.ntnu.item.ttm4115.termproject.ChatMessage;
import no.ntnu.item.ttm4115.termproject.DistributeGrChatToPlayers;
import no.ntnu.item.ttm4115.termproject.NewPlayerInGroupChat;
import no.ntnu.item.ttm4115.termproject.InformPlayersOfNewChatter;
import no.ntnu.item.ttm4115.termproject.ReleasePlayerGroupChatAgent;
import no.ntnu.item.ttm4115.termproject.PlayerLeftGroupChat;
import no.ntnu.item.ttm4115.termproject.GetMyMainFrame;
import no.ntnu.item.ttm4115.termproject.ReturnMainFrameFromPA;


public class GroupChatAgentActions extends Object {

	public static void newRoleRequest(InitiateNewGroupChat signal, GroupChatAgentSM asm){
	    // TODO edit action statements
		asm.myName = signal.myName;
		asm.groupName = signal.myGroupName;
		asm.mainFrame = signal.mainFrame;
		System.out.println("Lol" +signal.myGroupAddress);
		asm.sendMessage(new RoleRequestMsg(signal.myGroupName+"/groupChatAgent", "GroupChatAgent"), signal.myGroupAddress);
	}

	public static void startGGCA(RoleConfirmMsg signal, GroupChatAgentSM asm){
	    // TODO edit action statements
		asm.gcagAddress = signal.getSenderRole();
		asm.chatGUI = new GroupChatGUI(asm.myName, asm.gcagAddress, asm, asm.mainFrame, asm.groupName);
		asm.chatGUI.setjLabelGrpChtInput(asm.groupName);
		asm.chatGUI.groupChatStartup();
		asm.chatExists = 1;
		asm.chatGUI.appendMeToList();
		asm.sendMessage(new EnableGCAforGroup(asm.myName), signal.getSenderRole());
	}

	@SuppressWarnings("unchecked")
	public static void informGAAgent(EnableGCAforGroup signal, GroupChatAgentSM asm){
	    // TODO edit action statements
		asm.chatPlayers.put(signal.starterName, signal.getSenderRole());
		
		
	asm.sendMessage(new GCAGstartedToGa(), asm.getContext().getParentAddress());
	}

	@SuppressWarnings("unchecked")
	public static void distributeChatMessage(ChatMessage signal, GroupChatAgentSM asm){
	    // TODO edit action statements
		asm.chatHashMap.put(asm.chatMessageID, signal.message);
		asm.chatMessageID++;
		
			Iterator<Map.Entry> iterator = asm.chatPlayers.entrySet().iterator();
			
			while(iterator.hasNext()){
				Map.Entry me = (Map.Entry)iterator.next();
				
				asm.sendMessage(new DistributeGrChatToPlayers(signal.message), (ActorAddress)me.getValue());
				
			}
			
			
	}

	public static void displayChatMessage(DistributeGrChatToPlayers signal, GroupChatAgentSM asm){
	    // TODO edit action statements
		asm.chatGUI.receiveChatMsg(signal.message);
		
	}

	@SuppressWarnings("unchecked")
	public static void confirmNewPlayer(NewPlayerInGroupChat signal, GroupChatAgentSM asm){
	    // TODO edit action statements
		
		
		asm.chatPlayers.put(signal.myName, signal.myGroupChatAddress);
		
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		String xmlChatPlayers = xstream.toXML(asm.chatPlayers);
		String xmlChatHashMap = xstream.toXML(asm.chatHashMap);
		
		
		Iterator<Map.Entry> iterator = asm.chatPlayers.entrySet().iterator();
		
		while(iterator.hasNext()){
			Map.Entry me = (Map.Entry)iterator.next();
			
			asm.sendMessage(new InformPlayersOfNewChatter(xmlChatPlayers, xmlChatHashMap, signal.myName, signal.groupName), (ActorAddress)me.getValue());
			
		}
		
		
	}

	public static void InformOfNewUser(InformPlayersOfNewChatter signal, GroupChatAgentSM asm){
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object o1 = xstream.fromXML(signal.xmlChatMessagesMap);
		HashMap chatMessagesMap = (HashMap)o1;
		
		Object o2 = xstream.fromXML(signal.xmlOnlineUsersMap);
		HashMap onlineUsersMap= (HashMap)o2;
		
		
		// TODO edit action statements
		asm.chatGUI.informOthersOfNewPlayer(signal.newChatterName);
		asm.chatGUI.appendChattersToList(onlineUsersMap);
	}

	public static boolean alreadyLoggedIn(InformPlayersOfNewChatter signal, GroupChatAgentSM asm){
	    // TODO edit guard statements
		if(asm.chatExists == 1){
			return true;
		}
		else {
			return false;
		}
	    
	}

	@SuppressWarnings("unchecked")
	public static void removePlayerFromMap(ReleasePlayerGroupChatAgent signal, GroupChatAgentSM asm){
	    asm.chatPlayers.remove(signal.myName);
	    
	    XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
	    
	    String xmlChatPlayers = xstream.toXML(asm.chatPlayers);
		
		Iterator<Map.Entry> iterator = asm.chatPlayers.entrySet().iterator();
			
		while(iterator.hasNext()){
			Map.Entry me = (Map.Entry)iterator.next();
			
			asm.sendMessage(new PlayerLeftGroupChat(signal.myName, xmlChatPlayers), (ActorAddress)me.getValue());
		}
			
	
	}

	public static void removePlayerFromGui(PlayerLeftGroupChat signal, GroupChatAgentSM asm){   
	    XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object o1 = (Object)xstream.fromXML(signal.xmlUsersChatting);
		HashMap usersChatting = (HashMap)o1;
		
		asm.chatGUI.removePlayerFromList(signal.leaverName, usersChatting);
	}

	public static void newGUIandUpdateChat(InformPlayersOfNewChatter signal, GroupChatAgentSM asm){
	    XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);	
			
		Object o1 = xstream.fromXML(signal.xmlChatMessagesMap);
		HashMap chatMessagesMap = (HashMap)o1;
		asm.tempChatMessagesMap = chatMessagesMap;
			
		Object o2 = xstream.fromXML(signal.xmlOnlineUsersMap);
		HashMap onlineUsersMap= (HashMap)o2;
		asm.tempOnlineUsersMap = onlineUsersMap;
			
		asm.groupName = signal.groupName;
		asm.chatExists = 1;
		asm.tempSenderRole = signal.getSenderRole();
		asm.tempNewChatterName = signal.newChatterName;
			
		asm.sendMessage(new GetMyMainFrame(), asm.getContext().getParentAddress());
	}

	public static void startUpSession(ReturnMainFrameFromPA signal, GroupChatAgentSM asm){
		
		asm.mainFrame = signal.mainFrame;
		asm.chatGUI = new GroupChatGUI(asm.tempNewChatterName, asm.tempSenderRole, asm, asm.mainFrame, asm.groupName);
		asm.chatGUI.setjLabelGrpChtInput(asm.groupName);
		asm.chatGUI.setPreviouslySaid(asm.tempChatMessagesMap);
		asm.chatGUI.appendChattersToList(asm.tempOnlineUsersMap);
	}



}
